﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework.Audio;

namespace ShootEm.Components
{
    class Sight:Movable
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);

        SoundEffect soundEffect;
        SoundEffect birdSound;
        public Sight(ShootEm game, int x, int y, string texture, int vitesse)
            : base(game, x, y, texture, vitesse) { }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            this.soundEffect = this.Game.Content.Load<SoundEffect>("Sounds/shoot");
            this.birdSound = this.Game.Content.Load<SoundEffect>("Sounds/birds");
            base.Initialize();
        }


        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // The avatar follows the mouse position
            MouseState mouseState = Mouse.GetState();
            this.x = mouseState.X-32;
            this.y = mouseState.Y-32;
            if (this.x < 0)
                this.x = 0;
            else if (this.x > 936)
                this.x = 936;

            if (this.y < 0)
                this.y = 0;
            else if (this.y > 536)
                this.y = 536;

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                soundEffect.Play();
                foreach (Target target in Game.Components.OfType<Target>())
                {
                    //MessageBox(new IntPtr(0), "Catch ya" + mouseState.Y + " "+target.y, "Title", 0);
                    if (mouseState.X >= target.x - 16 && mouseState.X <= target.x + 100 && mouseState.Y >= target.y - 8  && mouseState.Y <= target.y + 50)
                    {
                        birdSound.Play();
                        Game.Components.Remove(target);
                        Explosion ex = new Explosion((ShootEm)this.Game,target.x,target.y);
                        Game.Components.Add(ex);
                        Score score = Game.Components.OfType<Score>().First<Score>();
                        score.changeScore();
                        break;
                    }
                }
               
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            base.Draw(gameTime);
        }
    }

}
